package com.example.cs_jump;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.cs_jump.Fragment.CacheFragment;
import com.example.cs_jump.Fragment.CollectFragment;
import com.example.cs_jump.Fragment.DustFragment;
import com.example.cs_jump.Fragment.InferoFragment;
import com.example.cs_jump.Fragment.InfoFragment;
import com.example.cs_jump.Fragment.MirageFragment;
import com.example.cs_jump.Fragment.NukeFragment;
import com.example.cs_jump.Fragment.OverpassFragment;
import com.example.cs_jump.Fragment.TrainFragment;
import com.example.cs_jump.Fragment.VertigoFragment;

import com.example.cs_jump.Util.ReadJsonParseJson;
import com.google.android.material.navigation.NavigationView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    //权限组列表 联网 读写SD卡权限
    private String[] PERMISSIONS_ALL_NEED = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
    };

    public static final int PERMISSION_REQUEST_CODR = 10000;

    private DrawerLayout drawerLayout;
    private ImageView image_zhi, image_wechat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = View.inflate(this, R.layout.activity_main, null);
        setContentView(view);

        //sp轻量化存储 判断是否为第一次启动app 第一次启动会提示用户 此app需要的权限列表
        SharedPreferences sp = getSharedPreferences("requestPermission", MODE_PRIVATE);
        Boolean flag = sp.getBoolean("flag", false);

        //设置flag 当第为第一次启动 则展示消息框与获取权限提示
        if (flag == false) {
            showDialog();
            //将文件中的json字符串读取出来 并且将其转换为jsonBean对象 并且插入到数据库中 0为文件名称
            for (int i = 0; i < 8; i++) {
                ReadJsonParseJson.readJSONwithString(this, i);
            }
        }

        //toolbar 工具栏 查找控件
        Toolbar myToolbar = findViewById(R.id.my_toolbar);
        //deawer抽屉布局查找对象
        drawerLayout = findViewById(R.id.activity_drawer);
        //导航栏视图查找对下
        NavigationView navigationView = findViewById(R.id.nav_view);
//        //工具栏 设置导航图片
        //myToolbar.setNavigationIcon(R.drawable.ic_baseline_menu_24);
        //将Activity设置成支持 导航栏操作 setSupportActionBar(工具栏对象 );
        myToolbar.setTitle("CSGOWIKI.TOP");
        setSupportActionBar(myToolbar);

        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, myToolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                    new InfoFragment()).commit();
//            navigationView.setCheckedItem(R.id.action_collect); 此处为menu里面收藏item 但被删除 如果有bug 则恢复menu里collect
        }


    }

//
//    //menu控件的select点击事件 当用户选择应用栏中的某个项时，
//    // 系统会调用您 Activity 的 onOptionsItemSelected() 回调方法
//    //Toolbar menu 选择
//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//        return super.onOptionsItemSelected(item);
//    }

    //如果是在抽屉展开则返回上一层
    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    //创建菜单返回menu
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_main, menu);
//        return true;
//    }

    //单击侧边栏
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_info:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new InfoFragment()).commit();
                break;
            case R.id.nav_dust:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new DustFragment()).commit();
                break;
            case R.id.nav_mirage:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new MirageFragment()).commit();
                break;
            case R.id.nav_nuke:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new NukeFragment()).commit();
                break;
            case R.id.nav_cache:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new CacheFragment()).commit();
                break;
            case R.id.nav_overpass:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new OverpassFragment()).commit();
                break;
            case R.id.nav_train:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new TrainFragment()).commit();
                break;
            case R.id.nav_vertigo:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new VertigoFragment()).commit();
                break;
            case R.id.nav_inferno:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new InferoFragment()).commit();
                break;
            case R.id.nav_collect:
                getSupportFragmentManager().beginTransaction().replace(R.id.container_fragment,
                        new CollectFragment()).commit();
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    //显示权限对话框
    public void showDialog() {
        SharedPreferences sp = getSharedPreferences("requestPermission", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("flag", true);
        editor.commit();

        AlertDialog.Builder dialog = new AlertDialog.Builder(
                MainActivity.this,
                R.style.UpdateDialogStyle);

        dialog.setTitle("使用APP基础权限申请")
                .setMessage("确保APP正常运行请点击allow" + "\n" + "\n" + "CS_Jump仅获取联网,读写权限")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        reqPermission();
                    }
                });
        dialog.show();
    }

    //查询权限
    public void reqPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!EasyPermissions.hasPermissions(this, PERMISSIONS_ALL_NEED)) {
                EasyPermissions.requestPermissions(this, "需要的权限",
                        PERMISSION_REQUEST_CODR, PERMISSIONS_ALL_NEED);
            }
        }
    }

}


//异步请求AsyncTask进行请求网络数据
//    public void getResult() {
//        taskAsync = new TaskAsync(this);
//        taskAsync.execute(HttpConnectionFilter.mapUrl[1]);
//    }
//    public class TaskAsync extends AsyncTask<String, Integer, String> {
//        ProgressBar progressBar;
//        public TaskAsync(Context context) {
//        }
//        //线程前执行的任务操作
//        @Override
//        protected void onPreExecute() {
//            //查找进度条 绑定view 对象
//            progressBar = binding.progressBarGetResult;
//
//            super.onPreExecute();
//        }
//        //接受输入参数 执行任务耗时操作
//        @Override
//        protected String doInBackground(String... strings) {
//
//            return null;
//        }
//        @Override
//        protected void onCancelled(String s) {
//            super.onCancelled(s);
//        }
//        @Override
//        protected void onPostExecute(String s) {
//            super.onPostExecute(s);
//        }
//
//        @Override
//        protected void onProgressUpdate(Integer... values) {
//            super.onProgressUpdate(values);
//        }



